/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.barrientosjunior.massfilerenamer.parser.element;

import java.io.File;

/**
 *
 * @author Junior
 */
public class ExtensionTagElement implements Element{
    File file = null;

    public void setFile(File file) {
        this.file = file;
    }

    @Override
    /**
     * Returns the extension of the file.
     * Returns a empty string if the file doesnt have an extension.
     */
    public String toString() {
        String extension = "";
        String filename = file.getName();

        char c = filename.charAt(filename.length() - 4);

        //alows only files with 3 or 4 characters as extensions.
        if (isThereADot(filename)) {
            if (filename.charAt(filename.length() - 4) == '.'){
                extension = filename.substring(filename.length() - 4);
            } else if (filename.charAt(filename.length() - 5) == '.') {
                extension = filename.substring(filename.length() - 5);
            }
        }
        return extension;
    }

    /**
     * Checa se existe '.' no nome de arquivo.
     * @param chars
     * @return
     */
    boolean isThereADot(String chars){
        boolean thereAre = false;
        for (int i = chars.length() - 1; i >= 0 ; i--) {
            if (chars.charAt(i) == '.') {
                thereAre = true;
                break;
            }
        }
        return thereAre;
    }

}
